<?php
/**
 * Created by PhpStorm.
 * User: Tangyijun
 * Date: 2017/3/21
 * Time: 11:18
 */
namespace app\home\controller;
use app\models\Bases;

class User extends Base{
    /**
     * @return \think\response\Json
     * 忘记密码第一步
     */
    public function forgetPassword(){
        try{
            self::checkSubmit();    //验证访问方式
            $post = input('param.');
            self::checkCode($post['code'],'resetPassword',$post['username']);      //验证验证码是否正确
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult());
    }

    /**
     * @return \think\response\Json
     * 忘记密码第二步
     */
    public function forgetPwdNext(){
        $user = new Bases('user');
        try{
            self::checkSubmit();
            $post = input('param.');
            $validate = $this->validate($post,'User.forgetPassword'); //验证密码
            if($validate!==true){
                throw new \LogicException($validate,10017);
            }
            if($user->updateData(['password'=>password_hash($post['password'],true)],"username = {$post['username']}")===false){
                throw new \LogicException('操作失败',1009);
            }
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult());
    }

    /**
     * @return \think\response\Json
     * 退出登录
     */
    public function dropOut(){
        try{
            if(cookie('uid',null)===false){
                throw new \LogicException('退出失败',10023);
            }
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult());
    }
}